package Vista;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import Controlador.Sistema;
import Negocio.ItemMineral;
import Negocio.Mineral;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AltaLoteSimple extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel labelAreaSiembra;
	private JTextField fieldAreaSiembra;
	private JLabel labelMinPrincipal;
	private JLabel labelMinComponentes;
	private JButton botonGuardar;
	private JLabel labelMineralSelect;
	private JButton botonAgregar;
	private JLabel labelPorcentajeMineral;
	private JTextField fieldPorcentajeMineral;
	private JComboBox mineral;
	private JButton botonCancelar;
	private JTextField fieldMinPrincipal;
	private Collection <ItemMineral> itemsMineral;
	private Vector <Mineral> mine;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				AltaLoteSimple inst = new AltaLoteSimple();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public AltaLoteSimple() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);
			this.setTitle("Alta Cereal Lote Simple");
			{
				labelAreaSiembra = new JLabel();
				getContentPane().add(labelAreaSiembra);
				labelAreaSiembra.setText("Area de Siembra:");
				labelAreaSiembra.setBounds(34, 23, 92, 14);
			}
			{
				fieldAreaSiembra = new JTextField();
				getContentPane().add(fieldAreaSiembra);
				fieldAreaSiembra.setBounds(206, 20, 139, 21);
			}
			{
				labelMinPrincipal = new JLabel();
				getContentPane().add(labelMinPrincipal);
				labelMinPrincipal.setText("Mineral Principal:");
				labelMinPrincipal.setBounds(34, 62, 141, 14);
			}
			{
				fieldMinPrincipal = new JTextField();
				getContentPane().add(fieldMinPrincipal);
				fieldMinPrincipal.setBounds(206, 58, 139, 21);
			}
			{
				labelMinComponentes = new JLabel();
				getContentPane().add(labelMinComponentes);
				labelMinComponentes.setText("Minerales Componentes...");
				labelMinComponentes.setBounds(34, 111, 141, 14);
			}
			{
				labelMineralSelect = new JLabel();
				getContentPane().add(labelMineralSelect);
				labelMineralSelect.setText("Mineral:");
				labelMineralSelect.setBounds(34, 147, 38, 14);
			}
			{
				mine= Sistema.getInstancia().getMinerales();
				//Armar texto
				Vector<String> nombres = new Vector<String>();
				nombres.add("  ");
				for (int i=0;i<mine.size();i++)
				{
					nombres.add(mine.elementAt(i).getNombre());
				}
				
				ComboBoxModel MineralesModel = new DefaultComboBoxModel(nombres);
				mineral = new JComboBox();
				getContentPane().add(mineral);
				mineral.setModel(MineralesModel);
				mineral.setBounds(111, 144, 112, 21);
			}
			{
				fieldPorcentajeMineral = new JTextField();
				getContentPane().add(fieldPorcentajeMineral);
				fieldPorcentajeMineral.setBounds(345, 144, 31, 21);
			}
			{
				labelPorcentajeMineral = new JLabel();
				getContentPane().add(labelPorcentajeMineral);
				labelPorcentajeMineral.setText("Porcentaje");
				labelPorcentajeMineral.setBounds(275, 147, 52, 14);
			}
			{
				botonAgregar = new JButton();
				getContentPane().add(botonAgregar);
				botonAgregar.setText("Agregar");
				botonAgregar.setBounds(421, 145, 53, 21);
				botonAgregar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("jButton1.actionPerformed, event="+evt);
						itemsMineral= new ArrayList <ItemMineral>();
						Long ni = (long)itemsMineral.size();
						int indice = mineral.getSelectedIndex()-1;
						Mineral m = mine.elementAt(indice);
						Double porcentaje = Double.parseDouble(fieldPorcentajeMineral.getText());
						ItemMineral itemM = new ItemMineral(ni,m.getCodigo(), porcentaje);
						itemsMineral.add(itemM);									
					}
				});
				
			}
			{
				botonGuardar = new JButton();
				getContentPane().add(botonGuardar);
				botonGuardar.setText("Guardar");
				botonGuardar.setBounds(163, 221, 75, 21);
				botonGuardar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						System.out.println("Guardar.actionPerformed, event="+evt);
						long cod = Sistema.getInstancia().generarCodigoLoteSimple();
						Double areaSiembra = Double.parseDouble(fieldAreaSiembra.getText());
						Mineral mineralPrincipal = Sistema.getInstancia().buscarMineral(fieldMinPrincipal.getText());
						Sistema.getInstancia().asignarLote(itemsMineral,cod);
						Sistema.getInstancia().AltaLoteSimple(cod, mineralPrincipal, itemsMineral, areaSiembra);	
					}
				});
			}
			{
				botonCancelar = new JButton();
				getContentPane().add(botonCancelar);
				botonCancelar.setText("Cancelar");
				botonCancelar.setBounds(289, 221, 75, 21);
			}
			pack();
			this.setSize(540, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
